local zeshu = fk.CreateSkill {
  name = "lingling__zeshu",
}

Fk:loadTranslationTable {
  ["lingling__zeshu"] = "择术",
  [":lingling__zeshu"] = "出牌阶段限一次，你可以获得一张【影】，然后选择：弃置两张牌，你获得一名其他角色一张牌；弃置一名其他角色两张牌，你交给其两张牌。",

  ["#lingling__zeshu"] = "择术：你可以获得一张【影】",
  ["lingling__zeshu_1"] = "弃置两张牌，你获得一名其他角色一张牌",
  ["lingling__zeshu_2"] = "弃置一名其他角色两张牌，你交给其两张牌",
  ["#lingling__zeshu-discard"] = "择术：请弃置两张牌！",
  ["#lingling__zeshu-choose-get-1"] = "择术：获得一名其他角色一张牌！",
  ["#lingling__zeshu-choose-give-1"] = "择术：交给 %dest 一张牌！",
  ["#lingling__zeshu-choose-discard-2"] = "择术：弃置一名其他角色两张牌！",
  ["#lingling__zeshu-choose-give-2"] = "择术：请交给 %dest 两张牌！",
}

local U = require "packages/lingling/util"

zeshu:addEffect("active", {
  anim_type = "drawcard",
  can_use = function(self, player)
    return player:usedSkillTimes(zeshu.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    if player.dead then return end
    local card = U.getShade(room, 1)
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, zeshu.name, nil, true, player)
    if player.dead then return end
    local choices = {}
    if #player:getCardIds("he") >= 2 then
      table.insert(choices, "lingling__zeshu_1")
    end
    if table.find(room:getOtherPlayers(player), function(p)
          return #p:getCardIds("he") >= 2
        end) then
      table.insert(choices, "lingling__zeshu_2")
    end
    if #choices == 0 then return end
    local choice = room:askToChoice(player, {
      choices = choices,
      all_choices = { "lingling__zeshu_1", "lingling__zeshu_2", },
      skill_name = zeshu.name,
    })
    if choice == "lingling__zeshu_1" then
      room:askToDiscard(player, {
        min_num = 2,
        max_num = 2,
        include_equip = true,
        skill_name = zeshu.name,
        prompt = "#lingling__zeshu-discard",
        cancelable = false,
      })
      local targets = table.filter(room:getOtherPlayers(player), function(p)
        return not p:isNude()
      end)
      if #targets == 0 then return end
      local tos = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = zeshu.name,
        prompt = "#lingling__zeshu-choose-get-1",
        cancelable = false,
      })
      if #tos == 1 then
        if tos[1].dead then return end
        room:obtainCard(player, room:askToChooseCard(player, {
          target = tos[1],
          flag = "he",
          skill_name = zeshu.name
        }), nil, fk.ReasonPrey, player, zeshu.name)
      end
    elseif choice == "lingling__zeshu_2" then
      local targets = table.filter(room:getOtherPlayers(player), function(p)
        return #p:getCardIds("he") >= 2
      end)
      if #targets == 0 then return end
      local tos = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = zeshu.name,
        prompt = "#lingling__zeshu-choose-discard-2",
        cancelable = false,
      })
      if #tos == 1 then
        local cards = room:askToChooseCards(player, {
          min = 2,
          max = 2,
          target = tos[1],
          flag = "he",
          skill_name = zeshu.name,
        })
        room:throwCard(cards, zeshu.name, tos[1], player)
        if player.dead or #player:getCardIds("he") < 2 then return end
        local c = room:askToCards(player, {
          min_num = 2,
          max_num = 2,
          include_equip = true,
          skill_name = zeshu.name,
          prompt = "#lingling__zeshu-choose-give-2::" .. tos[1].id,
          cancelable = false,
        })
        room:moveCardTo(c, Card.PlayerHand, tos[1], fk.ReasonGive, zeshu.name, nil, false, player)
      end
    end
  end,
})

return zeshu
